#include "git-head.h"

#include <cassert>

#include <git2/repository.h>

#include "util/logger/logger.h"

namespace git
{

Head::Head(Head &&b)
    : ref_(std::move(b.ref_)) {
}

Head &Head::operator=(Head &&b) {
    ref_ = std::move(b.ref_);
    return *this;
}

bool Head::operator==(const Head &b) const
{
    const LocalBranch *branchA = branch();
    const LocalBranch *branchB = b.branch();
    if (branchA && branchB) {
        return branchA->oidHex() == branchB->oidHex();
    }

    const Commit *commitA = commit();
    const Commit *commitB = b.commit();
    if (commitA && commitB) {
        return commitA->oidHex() == commitB->oidHex();
    }

    if (!(*this) && !b) {
        return true;
    } else {
        return false;
    }
}

std::string Head::oid() const
{
    if (const LocalBranch *b = branch()) {
        return b->oidHex();
    }
    if (const Commit *c = commit()) {
        return c->oidHex();
    }
    return std::string();
}

} // namespace git
